package com.leyou.lyelasticsearch.listener;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.leyou.lyelasticsearch.service.SearchService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class ElasticsearchRabbitMQListener {

    @Autowired
    private SearchService searchService;


    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "LEYOU.SEARCH.SAVE.QUEUE",durable = "true"),
                    exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
                    key = {"item.insert","item.update","item.saleable"}
            )
    )
    //保存
    public void save(Long id) throws JsonProcessingException {
            if(id==null){
                return;
            }
            searchService.save(id);
        }

    @RabbitListener(
            bindings = @QueueBinding(
                    value = @Queue(value = "LEYOU.SEARCH.DELETE.QUEUE",durable = "true"),
                    exchange = @Exchange(value = "LEYOU.ITEM.EXCHANGE",ignoreDeclarationExceptions = "true",type = ExchangeTypes.TOPIC),
                    key = {"item.delete"}
            )
    )
    //删除
    public void delete(Long id){
        if(id==null){
            return;
        }
        searchService.delete(id);
    }
}
